﻿Imports System.Web.DomainServices
Imports CupsApp.Data
Imports System.Web
Imports CupsApp.Service


Public Class DomainServicesFactory
    Implements IDomainServiceFactory


    Public Function CreateDomainService(ByVal domainServiceType As System.Type, ByVal context As System.Web.DomainServices.DomainServiceContext) As System.Web.DomainServices.DomainService Implements System.Web.DomainServices.IDomainServiceFactory.CreateDomainService
        Dim ctxt As New CupsDataContext()
        Dim clientRepo As IRepository(Of cups_Client)
        Dim interviewTemplateRepo As IRepository(Of cups_InterviewTemplate) = New LinqToSqlRepository(Of cups_InterviewTemplate)(New CupsDataContext())
        Dim interviewTemplateQuestionRepo As IRepository(Of cups_InterviewTemplateQuestion) = New LinqToSqlRepository(Of cups_InterviewTemplateQuestion)(New CupsDataContext())
        Dim interviewRepo As IRepository(Of cups_Interview) = New LinqToSqlRepository(Of cups_Interview)(New CupsDataContext())
        Dim interviewQuestionRepo As IRepository(Of cups_InterviewQuestion) = New LinqToSqlRepository(Of cups_InterviewQuestion)(New CupsDataContext())
        clientRepo = New LinqToSqlRepository(Of cups_Client)(ctxt)


        Dim ds As DomainService = New CupsDomainService 'Activator.CreateInstance(domainServiceType, New Object() {clientRepo, interviewTemplateRepo, interviewTemplateQuestionRepo, interviewRepo, interviewQuestionRepo})
        ds.Initialize(context)
        Return ds
    End Function

    Public Sub ReleaseDomainService(ByVal domainService As System.Web.DomainServices.DomainService) Implements System.Web.DomainServices.IDomainServiceFactory.ReleaseDomainService

    End Sub

    Public Shared Function CreateDomainService(ByVal httpCtxt As HttpContextBase) As CupsDomainService
        Dim sp As New HttpServiceProvider(httpCtxt)
        Dim csc As New DomainServiceContext(sp, DomainOperationType.Query)
        Return DomainService.Factory.CreateDomainService(GetType(CupsDomainService), csc)


    End Function
End Class
